document.addEventListener('DOMContentLoaded', function() {
    // 获取DOM元素
    const realDurationHours = document.getElementById('real-duration-hours');
    const realDurationMinutes = document.getElementById('real-duration-minutes');
    const videoDuration = document.getElementById('video-duration');
    const fps = document.getElementById('fps');
    const calculateButton = document.getElementById('calculate-timelapse');
    const resultDiv = document.getElementById('timelapse-result');
    const totalFramesElement = document.getElementById('total-frames');
    const intervalSecondsElement = document.getElementById('interval-seconds');
    const cameraSettingsElement = document.getElementById('camera-settings');

    // 计算按钮点击事件
    calculateButton.addEventListener('click', function() {
        // 获取输入值
        const hours = parseFloat(realDurationHours.value) || 0;
        const minutes = parseFloat(realDurationMinutes.value) || 0;
        const videoLength = parseFloat(videoDuration.value) || 10;
        const frameRate = parseFloat(fps.value) || 30;
        
        // 计算实际事件总时长（秒）
        const realDurationSeconds = (hours * 60 * 60) + (minutes * 60);
        
        // 计算总帧数（需要拍摄的照片数量）
        const totalFrames = Math.round(videoLength * frameRate);
        
        // 计算拍摄间隔（秒/帧）
        const intervalSeconds = realDurationSeconds / totalFrames;
        
        // 格式化间隔时间显示
        let intervalDisplay = '';
        if (intervalSeconds < 1) {
            // 如果间隔小于1秒，显示毫秒
            intervalDisplay = `${Math.round(intervalSeconds * 1000)} 毫秒`;
        } else if (intervalSeconds < 60) {
            // 如果间隔小于60秒，显示秒
            intervalDisplay = `${intervalSeconds.toFixed(1)} 秒`;
        } else if (intervalSeconds < 3600) {
            // 如果间隔小于1小时，显示分钟和秒
            const minutes = Math.floor(intervalSeconds / 60);
            const seconds = Math.round(intervalSeconds % 60);
            intervalDisplay = `${minutes} 分 ${seconds} 秒`;
        } else {
            // 如果间隔大于等于1小时，显示小时、分钟和秒
            const hours = Math.floor(intervalSeconds / 3600);
            const minutes = Math.floor((intervalSeconds % 3600) / 60);
            const seconds = Math.round(intervalSeconds % 60);
            intervalDisplay = `${hours} 小时 ${minutes} 分 ${seconds} 秒`;
        }
        
        // 相机设置建议
        let cameraSettings = '';
        if (intervalSeconds < 1) {
            cameraSettings = '需要特殊设备，标准相机可能无法支持毫秒级间隔';
        } else if (intervalSeconds < 30) {
            cameraSettings = `设置间隔定时器为 ${Math.ceil(intervalSeconds)} 秒，使用连续拍摄模式`;
        } else if (intervalSeconds < 60) {
            cameraSettings = `设置间隔定时器为 ${Math.ceil(intervalSeconds)} 秒，或使用遥控快门线`;
        } else {
            const minutes = Math.ceil(intervalSeconds / 60);
            cameraSettings = `设置间隔定时器为 ${minutes} 分钟，或使用可编程遥控快门线`;
        }
        
        // 显示结果
        totalFramesElement.textContent = totalFrames;
        intervalSecondsElement.textContent = intervalDisplay;
        cameraSettingsElement.textContent = cameraSettings;
        resultDiv.style.display = 'block';
    });
    
    // 输入值变化时隐藏结果
    const inputs = [realDurationHours, realDurationMinutes, videoDuration, fps];
    inputs.forEach(input => {
        input.addEventListener('change', function() {
            resultDiv.style.display = 'none';
        });
        input.addEventListener('keyup', function() {
            resultDiv.style.display = 'none';
        });
    });
});